import json
import os
from dotenv import load_dotenv

class Config:
    def __init__(self):
        # 加载.env文件中的环境变量
        load_dotenv()
        self.load_config()
    
    def load_config(self):
        with open('config.json', 'r') as f:
            config = json.load(f)
            
            self.email = config.get('email', {})
            self.email['password'] = os.getenv('EMAIL_PASSWORD', self.email.get('password', ''))



            # 加载 LLM 相关配置
            llm_config = config.get('llm', {})
            self.llm_model_type = llm_config.get('model_type', 'openai')
            self.openai_model_name = llm_config.get('openai_model_name', 'gpt-4o-mini')
            self.openai_base_url = llm_config.get('openai_base_url', None)  # 支持自定义OpenAI API base URL
            self.openai_api_key = os.getenv('OPENAI_API_KEY')  # 从环境变量获取OpenAI API密钥
            self.ollama_model_name = llm_config.get('ollama_model_name', 'llama3')
            self.ollama_api_url = llm_config.get('ollama_api_url', 'http://localhost:11434/api/chat')
            
            # 加载报告类型配置
            self.report_types = config.get('report_types', ["hacker_news_hours_topic", "hacker_news_daily_report", "readwise"])  # 默认报告类型
            
            # 加载 Slack 配置
            slack_config = config.get('slack', {})
            self.slack_webhook_url = slack_config.get('webhook_url')

            # 加载 Readwise 配置
            readwise_config = config.get('readwise', {})
            self.readwise_enabled = readwise_config.get('enabled', False)
            self.readwise_api_token = os.getenv('READWISE_API_TOKEN')
            self.readwise_default_location = readwise_config.get('default_location', 'feed')
            self.readwise_default_category = readwise_config.get('default_category', None)
            self.readwise_documents_limit = readwise_config.get('documents_limit', 15)
            self.readwise_days_back = readwise_config.get('days_back', 5)
            self.readwise_max_content_length = readwise_config.get('max_content_length', 1500)
            self.readwise_update_frequency = readwise_config.get('update_frequency_hours', 12)

            # 加载代理配置
            proxy_config = config.get('proxy', {})
            self.proxy_enabled = proxy_config.get('enabled', False)
            self.proxy_http = os.getenv('HTTP_PROXY', proxy_config.get('http'))
            self.proxy_https = os.getenv('HTTPS_PROXY', proxy_config.get('https'))
            # 如果只配置了一个代理，两个协议都使用同一个代理
            if self.proxy_http and not self.proxy_https:
                self.proxy_https = self.proxy_http
            elif self.proxy_https and not self.proxy_http:
                self.proxy_http = self.proxy_https
